﻿using System;
using System.Text;
using ZHDJ.Core.Util;
using ZHDJ.Data;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage.Master;

namespace ZHDJ.Web.Portal.MembersFindOrg
{
    public partial class MembersFindOrgInfo : UseMorePageMasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckLogin(true);
          
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        public void BindData()
        {
            var pickerurl = String.Format("../../app/Share/Picker/BranchToMemberPicker.aspx?ids=<script>{0}</script>",
                hdBranchID.GetValueReference());
            ppBranch.OnClientTrigger2Click = Window1.GetSaveStateReference(hdBranchID.ClientID, ppBranch.ClientID)
                                              + Window1.GetShowReference(pickerurl);

            StringBuilder js = new StringBuilder();
            js.AppendFormat(@"
                ; var pickerClientID = '{0}';
                  var pickerHdClientID = '{1}';
                  F(pickerClientID).setValue('');
                  F(pickerHdClientID).setValue('');
", ppBranch.ClientID, hdBranchID.ClientID);
            ppBranch.OnClientTrigger1Click = js.ToString();
        }

        private void LoadData()
        {
            if (CurrentUser.runSate != "-1")
            {
                RegisterJs("nottoLog();");
               
                return;
            }
            this.txtIDCard.Text = CurrentUser.IdentityCard;
            this.txtMobileNo.Text = CurrentUser.MobileNo;
            this.hdBranchID.Text = CurrentUser.BranchID;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            PartyMembersFindOrg member = new PartyMembersFindOrg();
            member.ID = StringHelper.NewID;
            member.UserName = txtUserName.Text.Trim();
            member.BranchID = hdBranchID.Text;
            member.Gender = rblGender.SelectedValue;
            member.Birthday = dpBirthday.Text;
            member.IdentityCard = txtIDCard.Text.Trim();
            member.MobileNo = txtMobileNo.Text.Trim();
            member.QQNumber = txtQQNumber.Text.Trim();
            member.NativePlace = txtNativePlace.Text.Trim();
            member.Nation = txtNation.Text.Trim();
            member.Educational = txtEducational.Text.Trim();
            member.Post = txtPost.Text.Trim();
            member.PartyCard = txtPartyCard.Text.Trim();
            if (!string.IsNullOrEmpty(dpJoinPartyDate.Text))
            {
                member.JoinPartyDate = Convert.ToDateTime(dpJoinPartyDate.Text);
            }
            member.Address = txtAddress.Text.Trim();
            member.PartyWorkingCommittee = PartyOrg.GetPartyAppointArray(hdBranchID.Text);
            member.PartyJobAddress = txtPartyJobAddress.Text.Trim();
            member.Status = "1";  //提交
            member.CreateBy = this.CurrentUser.LoginID;
            member.CreateTime = DateTime.Now;
            member.ModifyBy = this.CurrentUser.LoginID;
            member.ModifyTime = DateTime.Now;
            member.Remark = txtRemark.Text.Trim();
            member.OriginalBranch = txtOriginalBranch.Text.Trim();

            DB.PartyMembersFindOrgs.Add(member);
            DB.SaveChanges();
            //RedirectToDefault();

            RegisterJs("submitOk();");
        }

        #region 模块菜单
        /// <summary>
        /// 初始化module  如果通过url传参可不设置
        /// </summary>
        protected override void InitModule()
        {
            Module = "MembersFindOrg";
            Category = "QuickRegister";

        }
        #endregion
    }
}